package com.teach.copy;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Arrays;


public class Socket001 {
	private static final int PORT = 1000;
	public static void main(String[] args) {
		ServerSocket  serverSocket =  null;
		try {

			serverSocket =new ServerSocket(PORT);
		
			//服务器创建完成
			System.out.println("服务器创建完成"+serverSocket.getInetAddress().getHostAddress()+serverSocket.getLocalPort());
			
			while(true) {
				Socket client = serverSocket.accept();
				
				
				String clientInfo = String.format("客户端[%s%s]的消息",client.getInetAddress().getHostAddress(),client.getPort());
				//接受客户端的发送的文字
				
				Thread t1 = new Thread(()->{
					
					try {
						InputStream in = client.getInputStream();
						DataInputStream din = new DataInputStream(in);
						String msg = null;
						while(true) {
							msg = din.readUTF();
							System.out.println("接受了"+clientInfo);
						}
								
					} catch (Exception e) {
						System.out.format(clientInfo+"消息接受异常\n");
						e.printStackTrace();
					}				
				});
				t1.start();
				
				
				
				
//				System.out.format("关"+clientInfo);
//				client.close();//关闭客户端
				//serverSocket.close();
			}
		
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			
		}
		
	}
}
